package com.sxt.service.impl;

import com.sxt.common.CodeMsg;
import com.sxt.common.Constant;
import com.sxt.common.PageInfo;
import com.sxt.common.Result;
import com.sxt.dao.CustomerDao;
import com.sxt.pojo.Customer;
import com.sxt.service.ICustomerService;

import java.util.Map;

/**
 * @Description:
 * @author: Mr.T
 * @date 2020-11-04 14:29
 */
public class CustomerServiceImpl implements ICustomerService {
    CustomerDao customerDao = new CustomerDao();

    @Override
    public Result queryPage(Map<String, String> params, String page, String limit) {
        PageInfo<Customer> pageInfo = customerDao.selectPage(params, page, limit);
        return new Result(pageInfo);
    }

    @Override
    public Result add(String name, String phone, String sex, String company, Integer id) {
        //校验手机号的唯一性
        //根据手机号查询客户
        Customer customer = customerDao.selectCustomerByPhone(phone);
        if (customer != null){
            return  new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }
        //新增客户
        customerDao.insert(name,phone,sex,company,id);
        return new Result();
    }

    @Override
    public Result update(String id, String name, String phone, String sex, String company) {
        //校验手机号的唯一性
        //根据手机号查询客户
        Customer customer = customerDao.selectCustomerByPhone(phone);
        if (customer != null && !customer.getId().toString().equals(id)){
            return  new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }
        customerDao.update(id,name,phone,sex,company);
        return new Result();
    }

    @Override
    public Result delete(String id) {
        customerDao.updateDateted(id, Constant.CUSTOMER_DELETE_INVALID);
        return new Result();
    }

    @Override
    public Result setSaleman(String userId, String[] cIds) {
        customerDao.batchUpdateSalemanId(userId,cIds);
        return new Result();
    }
}
